﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WikiTrailCreator.SpecialDialogs
{
    public partial class FormAddLink : Form
    {
        public FormAddLink()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.ValidateChildren();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        public string GetLinkAsWikiText()
        {
            string wikiText = "";

            if (checkBoxExternalLink.Checked == true)
            {
                if (textBoxCaption.Text != null && textBoxCaption.Text != String.Empty)
                {
                    wikiText = "[" + textBoxLink.Text + " " + textBoxCaption.Text + "]" + Environment.NewLine;
                }
                else
                {
                    wikiText = "[" + textBoxLink.Text + "]" + Environment.NewLine;
                }
            }
            else
            {
                if (textBoxCaption.Text != null && textBoxCaption.Text != String.Empty)
                {
                    wikiText = "[[" + textBoxLink.Text + "|" + textBoxCaption.Text + "]]" + Environment.NewLine;
                }
                else
                {
                    wikiText = "[[" + textBoxLink.Text + "]]" + Environment.NewLine;
                }
            }

            return wikiText;
        }

        private void textBoxLink_Validating(object sender, CancelEventArgs e)
        {
            if (textBoxLink.Text != null && textBoxLink.Text.Length > 0)
            {
                errorProvider1.SetError(textBoxLink, "");
            }
            else
            {
                errorProvider1.SetError(textBoxLink, "You must enter a page to link to.");
                e.Cancel = true;
            }
        }
    }
}
